// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include "olap/rowset/segment_v2/inverted_index/util/docid_set_iterator.h"

namespace doris::segment_v2::inverted_index {

class PhrasePositions {
public:
    PhrasePositions(DISI postings, int32_t o, int32_t ord, std::vector<std::string> terms)
            : _offset(o), _ord(ord), _postings(std::move(postings)), _terms(std::move(terms)) {}

    void first_position() {
        _count = visit_node(_postings, Freq {});
        next_position();
    }

    bool next_position() {
        if (_count-- > 0) {
            _position = visit_node(_postings, NextPosition {}) - _offset;
            return true;
        } else {
            return false;
        }
    }

    int32_t _position = 0;
    int32_t _count = 0;
    int32_t _offset = 0;
    int32_t _ord = 0;
    DISI _postings;
    PhrasePositions* _next = nullptr;
    int32_t _rpt_group = -1;
    int32_t _rpt_ind = 0;
    std::vector<std::string> _terms;
};

} // namespace doris::segment_v2::inverted_index
